package com.louis.iread.bean;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;

/**
 * Created by louis on 17-3-20.
 */

public class WebsiteMgr {
    public List<Website> getWebsites() {
        return websites;
    }

    public void setWebsites(List<Website> websites) {
        this.websites = websites;
        hashWebsites = new HashMap<>();
        if (websites == null) {
            return;
        }
        for (Website website : websites) {
            try {
                hashWebsites.put(new URL(website.getHost()).getHost(), website);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }

    public Website getWebsite(String host) {
        if (host != null && host.startsWith("http")) {
            try {
                return hashWebsites.get(new URL(host).getHost());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        return hashWebsites.get(host);
    }

    private List<Website> websites;
    private HashMap<String, Website> hashWebsites;

    public Website getMainSite() {
        return mainSite;
    }

    public void setMainSite(Website mainSite) {
        this.mainSite = mainSite;
    }

    private Website mainSite;

    private static WebsiteMgr sManager = null;

    public static WebsiteMgr getManager() {
        if (sManager == null) {
            sManager = new WebsiteMgr();
        }
        return sManager;
    }
}
